package com.my.study.guava;

import com.google.common.cache.CacheBuilder;
import com.google.common.cache.CacheLoader;
import com.google.common.cache.LoadingCache;
import com.google.common.collect.Lists;
import org.junit.Test;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

/**
 * 缓存测试类
 *
 * 参考博客：https://www.cnblogs.com/aspirant/p/11734918.html
 *
 * @author: yidujun
 * @create: 2021/12/08 16:27
 */
public class CacheTest {

    @Test
    public void buildTest() throws ExecutionException {
        LoadingCache<String, List<String>> cache = CacheBuilder.newBuilder()
                .maximumSize(1000)
                .expireAfterWrite(10000, TimeUnit.SECONDS)
                .build(new CacheLoader<String, List<String>>() {
                    @Override
                    public List<String> load(String key) throws Exception {
                        return loadValue(key);
                    }
                });

        List<String> stringList = cache.get("1");

        List<String> list = cache.getIfPresent("1");

        System.out.println(list);
    }

    private List<String> loadValue(String key) {
        return Lists.newArrayList(key);
    }

}
